
from huobi.client.account import AccountClient
from huobi.client.generic import GenericClient
from huobi.client.market import MarketClient
from huobi.client.trade import TradeClient
from huobi.constant.definition import AccountBalanceUpdateType, OrderSource, OrderType
from huobi.model.generic.symbol import Symbol
from huobi.utils.log_info import Log
from huobi.privateconfig import *

class QuantSymbol:
    def __init__(self, account_client:AccountClient, generic_client:GenericClient,
         trade_client:TradeClient, market_client:MarketClient) -> None:
        self.m_symbol:Symbol = None
        self.m_die = False

        self.m_account_client = account_client
        self.m_generic_client = generic_client
        self.m_trade_client = trade_client
        self.m_market_client = market_client


    def update(self):
        pass

    def die(self)-> bool:
        return self.m_die

    def set_die(self,die:bool):
        self.m_die = die

    def ch2symbol(self, ch):
        return ch.replace("market.","").replace(".trade.detail","")

    def create_order(self, symbol:str, price:float, amount:float, order_type:OrderType):
        Log.info("try create order symbol={0}, price={1}, amount={2}, order_type={3}".format(symbol,price,amount,order_type))
        orderid = 0
        orderid = self.m_trade_client.create_order(symbol,p_account_id,order_type,amount,price,source=OrderSource.API)
        Log.info("create order orderid = {0}".format(orderid))
        return orderid

    def get_balance(self,currency):
        balances = self.m_account_client.get_balance(p_account_id)
        for balance in balances:
            if balance.currency == currency and balance.type == AccountBalanceUpdateType.TRADE:
                return float(balance.balance)
        
        return 0